package com.yww.leetcode2.hashtable;

import java.util.Arrays;
import java.util.HashSet;

/**
 * @author yww
 * @description 349. 两个数组的交集
 * @since 2024/11/12
 */
public class Intersection349_02 {
    public int[] intersection(int[] nums1, int[] nums2) {
        HashSet<Integer> hashtable = new HashSet<Integer>();
        HashSet<Integer> res = new HashSet<Integer>();

        for (int i : nums1) {
            hashtable.add(i);
        }
        for (int i : nums2) {
            if (hashtable.contains(i)) {
                res.add(i);
            }
        }
        return res.stream().mapToInt(i -> i).toArray();
    }

    public int[] intersection2(int[] nums1, int[] nums2) {
        int[] hashtable = new int[1005];
        HashSet<Integer> res = new HashSet<>();
        for (int i = 0; i < nums1.length; i++) {
            hashtable[nums1[i]] = 1;
        }
        for (int i = 0; i < nums2.length; i++) {
            if (hashtable[nums2[i]] == 1) {
                res.add(nums2[i]);
            }
        }
        return res.stream().mapToInt(i -> i).toArray();
    }

    public static void main(String[] args) {
        int[] ints = new Intersection349_02().intersection2(new int[]{1, 2, 2, 1}, new int[]{2, 2});
        System.out.println(Arrays.toString(ints));
    }
}
